﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using zUfDsl.Common;
using zUfDsl.Models;

namespace zUfDsl.Sync
{
    public partial class FrmLog : Form
    {
        private Models.tipConfig _tipconfig;
        private Common.HandleDataGrid _handleDataGrid;
        private delegate void SetValGrid<T>(List<T> list);
        private delegate string GetControlValue(Control c);
        private delegate void InitControl(Control main, DataGridView dg, string repcode, Models.tipConfig tip);
        private delegate T GetVal<T>(Control c);
        private delegate List<T> GetValList<T>(DataGridView c);
        // private List<Model.v_prepareforpz> _l_data;

        public FrmLog()
        {
            InitializeComponent();
            this.Icon = Properties.Resources.Fly1;
            this.Text = "同步日志";
            this.dDate_1.Value = DateTime.Now.AddDays(-1);
            this.dDate_2.Value = DateTime.Now;
            this.tbSource.Text = cHandleUF.cAccID;
            _handleDataGrid = new HandleDataGrid();
            _handleDataGrid.InitControlCombol(this.cDataType, "存货档案;BOM表", "", true);
            _handleDataGrid.InitControlCombol(this.cbSuc, "是;否", "", true);

            #region 初始化dataGrid的格式
            var lcolset = new List<reportColumns>();

            var tmpcolindex = new reportColumns() { Code = "rowindex", Title = "行号", Align = 2, Width = 60, bModify = false, iOrder = 1 };
            lcolset.Add(tmpcolindex);
            //tmpcolindex = new reportColumns() { Code = "rowindex", Title = "行号", Align = 2, Width = 60, bModify = false, iOrder = 1 };
            //lcolset.Add(tmpcolindex);
            var tmpcol = new reportColumns() { Code = "cSyncPerson", Title = "同步人", Align = 2, Width = 80, bModify = false, iOrder = 3 };
            lcolset.Add(tmpcol);
            tmpcol = new reportColumns() { Code = "dDateStart", Title = "开始时间", cFormat = "yy-MM-dd HH:mm:ss", Align = 1, Width = 120, bModify = false, iOrder = 4 };
            lcolset.Add(tmpcol);
            tmpcol = new reportColumns() { Code = "dSucDate", Title = "完成时间", Align = 1, cFormat = "yy-MM-dd HH:mm:ss", Width = 120, bModify = false, iOrder = 5 };
            lcolset.Add(tmpcol);
            tmpcol = new reportColumns() { Code = "cSourceAccount", Title = "源账套", Align = 1, Width = 80, bModify = false, iOrder = 6 };
            lcolset.Add(tmpcol);
            tmpcol = new reportColumns() { Code = "cDescAccount", Title = "接收账套", Align = 1, Width = 80, bModify = false, iOrder = 8 };
            lcolset.Add(tmpcol);
            tmpcol = new reportColumns() { Code = "cDateType", Title = "数据类型", Align = 1, Width = 80, bModify = false, iOrder = 8 };
            lcolset.Add(tmpcol);
            tmpcol = new reportColumns() { Code = "cMemo", Title = "结果", Align = 1, Width = 200, bModify = false, iOrder = 9 };
            lcolset.Add(tmpcol);
            tmpcol = new reportColumns() { Code = "bSuc", Title = "成功", Align = 1, Width = 80, bModify = false, iOrder = 9 };
            lcolset.Add(tmpcol);
            _handleDataGrid.InitGrid(this.superGridSum1, lcolset);
            #endregion
        }

        private void DisplaySel_Click(object sender, EventArgs e)
        {
            Common.HandleButton.SyncButton(this.DisplaySel, "查询", ref this._tipconfig, btQuery_Click_Proc);
        }

        #region 获取数据

        private void btQuery_Click_Proc()
        {
            string err = "";
            try
            {
                

                using (var db=new HandleDb().GetUfsystem())
                {
                    var delval = new GetControlValue(_handleDataGrid.GetControlValue);
                    var ddate1 = this.Invoke(delval, this.dDate_1).ObjToDate();
                    var ddate2 = this.Invoke(delval, this.dDate_2).ObjToDate().AddDays(1);
                    var l_log = db.LogSyncUf.Where(s => s.dDateStart >= ddate1 && s.dDateStart <= ddate2);
                    var val = this.Invoke(delval, this.tbSource).ObjToString();
                    if (val.Length>0)
                    {
                        l_log = l_log.Where(s => s.cSourceAccount == val);
                    }
                    var val1 = this.Invoke(delval, this.tbDesc).ObjToString();
                    if (val1.Length > 0)
                    {
                        l_log = l_log.Where(s => s.cDescAccount == val1);
                    }
                    var val2 = this.Invoke(delval, this.cDataType).ObjToString();
                    if (val2.Length>0)
                    {
                        l_log = l_log.Where(s => s.cDateType== val2);
                    }
                    var val3 = this.Invoke(delval, this.cbSuc).ObjToString();
                    if (val3.Length > 0)
                    {
                        if(val3=="是")
                        {
                            l_log = l_log.Where(s => s.bSuc == true);
                        }
                        else
                        {
                            l_log = l_log.Where(s => s.bSuc == false);
                        }
                    }

                    var l_val = l_log.OrderByDescending(s => s.AutoID).ToList();
                    var delset = new SetValGrid<ModelUF.LogSyncUf>(_handleDataGrid.BlindData);
                    this.Invoke(delset, l_val);        
                }
                return;
            }
            catch (Exception ex)
            {
                err = string.Format("【{0}】==={1}", ex.Message, ex.StackTrace);
                goto ErrHandle;
            }
        ErrHandle:
            _tipconfig.bSuc = false;
            _tipconfig.cCaption = "出现错误";
            _tipconfig.cMsg = err;
        }
        #endregion

        private void FrmLog_SizeChanged(object sender, EventArgs e)
        {
            if(this.Width>150)
            {
                this.superGridSum1.Width = this.Width - 100;
            }
            if (this.Height>300)
            {
                this.superGridSum1.Height = this.Height - 200;
            }
        }

        private void Export_Click(object sender, EventArgs e)
        {
            _handleDataGrid.ExportExcel("同步日志");
        }
    }
}
